package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/23 16:37
 */
@Service(interfaceClass = RoleSevice.class)
@Transactional
public class RoleServiceImpl implements RoleSevice {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public void add(Role role, Integer[] menuIds,Integer[] permissionIds) {
        roleDao.add(role);
        Integer id = role.getId();
        //通过roleId和menuId创建中间表的关系
        for (Integer menuId : menuIds) {
            roleDao.addByRoleidAndMenuid(id, menuId);
        }
        for (Integer permissionId : permissionIds) {
            roleDao.addByRoleidAndpermissionId(id, permissionId);
        }

    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteMenuIdsByRoleId(id);
        roleDao.deletePermissionIdsByRoleId(id);
        roleDao.delete(id);
    }

    @Override
    public void update(Role role, Integer[] menuIds,Integer[] permissionIds) {
        roleDao.update(role);
        Integer id = role.getId();
        //根据id删除中间表的关系
        roleDao.deleteMenuIdsByRoleId(id);
        roleDao.deletePermissionIdsByRoleId(id);
        //通过roleId创建中间表的关系
        for (Integer menuId : menuIds) {
            roleDao.addByRoleidAndMenuid(id, menuId);
        }
        for (Integer permissionId : permissionIds) {
            roleDao.addByRoleidAndpermissionId(id, permissionId);
        }
    }

    @Override
    public Role get(Integer id) {
        Role role=roleDao.getById(id);
        return role;
    }

    @Override
    public PageResult getByConditions(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Role> roleList = roleDao.getByConditions(queryPageBean.getQueryString());
        PageInfo info = new PageInfo(roleList);
        PageResult pageResult = new PageResult(info.getTotal(),info.getList());
        return pageResult;
    }

    /**
     * 获取全部的菜单和权限信息
     * @return
     */
    @Override
    public Map<String, Object> getAll() {
        List<Menu> menuList=menuDao.getAll();
        List<Permission> permissionList = permissionDao.getAll();
        Map<String, Object> map = new HashMap<>();
        map.put("menuData", menuList);
        map.put("permissionData", permissionList);
        return map;
    }

    /**
     * 查询所有中间表的ids
     * @param id
     * @return
     */
    @Override
    public Map<String, List<Integer>> getMenuIdsAndPermissionIdsById(Integer id) {
        //通过roleId查询关联的ids
        List<Integer> menuIds=roleDao.getMenuIdsByRoleId(id);
        List<Integer> permissionIds=roleDao.getPermissionIdsByRoleId(id);
        Map<String, List<Integer>> map = new HashMap<>();
        map.put("menuIds",menuIds);
        map.put("permissionIds",permissionIds);
        return map;
    }
}
